package com.sgb.kjwl.viewmodel.transport;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapGoodsBiddingEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.TransportRepertory;
import com.swgk.core.event.EventConstant;
import com.swgk.core.event.MenuEvent;
import com.sgb.kjwl.view.ui.activity.transport.OrderBargainingActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OrderBargainingModel extends BaseViewModel {

    public ObservableField<MapGoodsBiddingEntity> sourceDetailEntity = new ObservableField<>(new MapGoodsBiddingEntity());
    private OrderBargainingActivity activity;
    private TransportRepertory repertory;
    private int page = 1;
    private int size = 10;

    public List<GoodsInfoTypeEntity3> sourcesType;
    public List<GoodsInfoTypeEntity3> carsType;
    public List<GoodsInfoTypeEntity3> carsLengthType;
    public List<GoodsInfoTypeEntity3> transportType;
    public List<GoodsInfoTypeEntity3> handlingType;
    public List<GoodsInfoTypeEntity3> payType;
    public List<GoodsInfoTypeEntity3> invoiceType;


    public OrderBargainingModel(OrderBargainingActivity activity, TransportRepertory repertory){
        this.activity = activity;
        this.repertory = repertory;
    }

    /**
     * 选择货源
     */
    public void getMyGoodsSourceList(boolean isLoadMore){
        DialogHelper.showProgressDialog(activity, "加载中", 0, false);
        page = isLoadMore ? page + 1 : 1;
        APIManager.getInstance().getTransAPI().getMyGoodsSourceList(size,page).enqueue(new Callback<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> call, Response<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (!response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, response.body().getMsg());
                    return;
                }

                activity.setData(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void bargining(){
        DialogHelper.showProgressDialog(activity, "提交中", 0, false);
        APIManager.getInstance().getTransAPI().counterOffer(sourceDetailEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (!response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, response.body().getMsg());
                    return;
                }

                activity.finish();

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void ordering(){
        DialogHelper.showProgressDialog(activity, "提交中", 0, false);
        APIManager.getInstance().getTransAPI().placeOrder(sourceDetailEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (!response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, response.body().getMsg());
                    return;
                }
                EventBus.getDefault().postSticky(MenuEvent.make().setMessageTag(EventConstant.EVENT_ORDER_FINISH));
                activity.finish();
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.displayRole(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //货物类型
    public void getGoodsType(){
        NewAPIManager.getInstance().getLogisticsAPI().getAllGoodsType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
//                    Log.v("responsBody",new Gson().toJson(response.body().getData()));
                    sourcesType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.GOODS_INFO_TYPE,new Gson().toJson(response.body().getData()));
                } else {
//                    MToast.showToast(activity, response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //货车类型
    public void getCarsType(){
        APIManager.getInstance().getDriverAPI().getCarType("").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    carsType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //货车长度
    public void getCarsLength(){
        APIManager.getInstance().getDriverAPI().getCarLenType("").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    carsLengthType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_LENGTH,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //运输方式
    public void getTransType(){
        APIManager.getInstance().getDriverAPI().getTransType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    transportType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.TRANSPORT_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //装载方式
    public void getHandleType(){
        APIManager.getInstance().getDriverAPI().getHandleType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    handlingType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_HANDLING_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //付款方式
    public void getPayType(){
        GoodsInfoTypeEntity3 one = new GoodsInfoTypeEntity3();
        GoodsInfoTypeEntity3 two = new GoodsInfoTypeEntity3();
        List<GoodsInfoTypeEntity3> list = new ArrayList<>();
        one.setName("发货人支付（现付）");
        one.setNo("1");
        two.setName("收货人支付（到付）");
        two.setNo("0");
        list.add(one);
        list.add(two);
        payType=list;
/*        APIManager.getInstance().getDriverAPI().getPayType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    payType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PAY_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });*/
    }

    //发票方式
    public void getInvoiceType(){
        APIManager.getInstance().getDriverAPI().getInvoiceType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    invoiceType = response.body().getData();
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.INVOICE_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //是否自卸
    public void isDump() {
        CarriageSelectEntity one = new CarriageSelectEntity();
        CarriageSelectEntity two = new CarriageSelectEntity();
        List<CarriageSelectEntity> list = new ArrayList<>();
        one.setName("是");
        one.setNo("1");
        two.setName("否");
        two.setNo("0");
        list.add(one);
        list.add(two);
        activity.showIsDumpPop(list);
    }
    //获取经纬度信息
    public void getLocationLL(String provinceNo,String cityNo,String districtNo,int type) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getLocationLL(response.body().getData(),type);
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });

    }



}
